package com.ruoyi.web.controller.price;

import java.util.List;

import cn.hutool.json.JSONUtil;
import com.ruoyi.basic.domain.TbDailyUnitPrice;
import com.ruoyi.price.domain.TbProject;
import com.ruoyi.price.service.ITbProjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工程信息Controller
 * 
 * @author ruoyi
 * @date 2022-09-04
 */
@Controller
@RequestMapping("/price/project")
public class TbProjectController extends BaseController
{
    private String prefix = "price/project";

    @Autowired
    private ITbProjectService tbProjectService;

    @RequiresPermissions("price:project:view")
    @GetMapping()
    public String project(ModelMap map)
    {
        map.put("userId", getSysUser().getLoginName());
        map.put("admin", getSysUser().isAdmin());
        return prefix + "/project";
    }

    /**
     * 查询工程信息列表
     */
    @RequiresPermissions("price:project:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbProject tbProject)
    {
        startPage();
        List<TbProject> list = tbProjectService.listByCondition(tbProject,getSysUser());
        return getDataTable(list);
    }

    /**
     * 导出工程信息列表
     */
    @RequiresPermissions("price:project:export")
    @Log(title = "工程信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TbProject tbProject)
    {
        List<TbProject> list = tbProjectService.listByCondition(tbProject,getSysUser());
        ExcelUtil<TbProject> util = new ExcelUtil<TbProject>(TbProject.class);
        return util.exportExcel(list, "工程信息数据");
    }

    /**
     * 新增工程信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 计算单价
     */
    @GetMapping("/cal")
    @RequiresPermissions("price:project:view")
    public String cal()
    {
        return prefix + "/cal";
    }

    /**
     * 新增保存工程信息
     */
    @RequiresPermissions("price:project:add")
    @Log(title = "工程信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated TbProject tbProject)
    {
        return toAjax(tbProjectService.saveProject(tbProject));
    }

    /**
     * 修改工程信息
     */
    @RequiresPermissions("price:project:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TbProject tbProject = tbProjectService.getProjectById(id);
        mmap.put("tbProject", tbProject);
        return prefix + "/edit";
    }

    /**
     * 修改保存工程信息
     */
    @RequiresPermissions("price:project:edit")
    @Log(title = "工程信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated TbProject tbProject)
    {

        return toAjax(tbProjectService.updateProject(tbProject));
    }

    /**
     * 删除工程信息
     */
    @RequiresPermissions("price:project:remove")
    @Log(title = "工程信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tbProjectService.deleteByIds(ids));
    }

    /**
     * 引用功能
     * @param type
     * @param currencyType
     * @param beginDate
     * @param endDate
     * @param categoryId
     * @param mmap
     * @return
     */
    @GetMapping("/select")
    public String select(int type,String currencyType,String beginDate,String endDate,String categoryId, ModelMap mmap)
    {
        String url = prefix + "/dialog/select"+type;
        mmap.put("type",type);
        mmap.put("currencyType",currencyType);
        mmap.put("beginDate",beginDate);
        mmap.put("endDate",endDate);
        mmap.put("categoryId",categoryId);
        return url;
    }

    /**
     * 计算
     */
    @PostMapping("/cal")
    @ResponseBody
    public AjaxResult cal(@Validated TbProject tbProject)
    {
        return AjaxResult.success(tbProjectService.cal(tbProject));
    }

    /**
     * 中外用工比+中外工效比提示弹框
     * @param type
     * @return
     */
    @RequiresPermissions("price:project:view")
    @GetMapping("/inputAlter")
    public String inputAlter(int type)
    {
        return prefix + "/dialog/alterDialog" + type;
    }
}

